package net.gz01.wallet.web.controller.api;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class ParamUtil {
	private static final String SIGN_PARAM_SEPARATOR = "&";

	public static TreeMap<String, String> getParamMap(HttpServletRequest request) {
		TreeMap<String, String> transMap = new TreeMap<String, String>();
		Enumeration<String> enu = request.getParameterNames();
		String t = null;
		while (enu.hasMoreElements()) {
			t = enu.nextElement();
			transMap.put(t, request.getParameter(t));
		}
		return transMap;
	}

	public static Map<String, String> getParamsMap(String queryString, String enc) {
		Map<String, String> paramsMap = new TreeMap<String, String>();
		if (queryString != null && queryString.length() > 0) {
			int ampersandIndex, lastAmpersandIndex = 0, tmpIndex = 0;
			String subStr, param, value;
			do {
				ampersandIndex = queryString.indexOf('&', lastAmpersandIndex) + 1;
				if (ampersandIndex > 0) {
					subStr = queryString.substring(lastAmpersandIndex, ampersandIndex - 1);
					lastAmpersandIndex = ampersandIndex;
				} else {
					subStr = queryString.substring(lastAmpersandIndex);
				}

				tmpIndex = subStr.indexOf('=');
				param = subStr.substring(0, tmpIndex);
				value = subStr.substring(tmpIndex + 1);
				try {
					value = URLDecoder.decode(value, enc);
				} catch (UnsupportedEncodingException ignored) {
				}
				if (!"".equals(param))
					paramsMap.put(param, value);
			} while (ampersandIndex > 0);
		}
		return paramsMap;
	}

	public static String getSignMsg(Map map) {
		StringBuffer sb = new StringBuffer();
		String key = "";
		for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) {
			key = it.next();
			sb.append(key).append("=").append(map.get(key)).append(SIGN_PARAM_SEPARATOR);
		}
		if (sb.indexOf(SIGN_PARAM_SEPARATOR) > -1)
			sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

	public static TreeMap toTreeMap(Map map) {
        TreeMap treeMap = null;
        if (map != null) {
            treeMap = new TreeMap();
            Object obj = null;
            for (Iterator it = map.keySet().iterator(); it.hasNext(); ) {
                obj = it.next();
                treeMap.put(obj, map.get(obj));
            }
        }

        return treeMap;
    }

	public static void main(String[] args) {
		String str = "k1=v1&k2=v2===&k3=v3";

		System.out.println(ParamUtil.getParamsMap(str, "utf-8"));
	}

}
